import cv2
import numpy as np
from matplotlib import pyplot as plt


class ImageContrast:

    def __init__(self, image1, image2):
        self.image1 = cv2.imread(image1)
        self.image2 = cv2.imread(image2)
        self.image1 = cv2.resize(self.image1, (256,256))
        self.image2 = cv2.resize(self.image2, (256,256))
        # cv2.namedWindow("img1", cv2.WINDOW_AUTOSIZE)
        # cv2.namedWindow("img2", cv2.WINDOW_AUTOSIZE)
        #
        # cv2.imshow('img1', self.image1)
        # cv2.imshow('img2', self.image2)

    # 灰度直方图对比相识度
    def classify_gray_hist(self, size=(256,256)):
        image1 = cv2.resize(self.image1, size)
        image2 = cv2.resize(self.image2, size)

        hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
        hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])

        # 比较直方图
        plt.plot(range(256), hist1, 'r')
        plt.plot(range(256), hist2, 'b')
        plt.show()

        degree = 0
        for i in range(len(hist1)):
            if hist1[i] != hist2[i]:
                degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
            else:
                degree = degree + 1
        degree = degree / len(hist1)
        return degree

    # 计算单通道的直方图的相似值
    def calculate(self, image1, image2):
        hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
        hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
        # 计算直方图的重合度
        degree = 0
        for i in range(len(hist1)):
            if hist1[i] != hist2[i]:
                degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
            else:
                degree = degree + 1
        degree = degree / len(hist1)
        return degree

    def classify_hist_with_split(self, size=(256, 256)):
        # 将图像resize后，分离为三个通道，再计算每个通道的相似值
        image1 = cv2.resize(self.image1, size)
        image2 = cv2.resize(self.image2, size)
        sub_image1 = cv2.split(image1)
        sub_image2 = cv2.split(image2)
        sub_data = 0
        for im1, im2 in zip(sub_image1, sub_image2):
            sub_data += self.calculate(im1, im2)
        sub_data = sub_data / 3
        return sub_data

    def classify_aHash(self):
        image1 = cv2.resize(self.image1, (8, 8))
        image2 = cv2.resize(self.image2, (8, 8))
        gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
        gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
        hash1 = self.getHash(gray1)
        hash2 = self.getHash(gray2)
        return self.Hamming_distance(hash1, hash2)

    # 输入灰度图，返回hash
    @staticmethod
    def getHash(image):
        avreage = np.mean(image)
        hash = []
        for i in range(image.shape[0]):
            for j in range(image.shape[1]):
                if image[i, j] > avreage:
                    hash.append(1)
                else:
                    hash.append(0)
        return hash

    # 计算汉明距离
    @staticmethod
    def Hamming_distance(hash1, hash2):
        num = 0
        for index in range(len(hash1)):
            if hash1[index] != hash2[index]:
                num += 1
        return num


class ImageRecognition:
    pass

#
# if __name__ == '__main__':
#     ic = ImageContrast('image2.jpg', 'image5.jpg')
#     # degree1 = ic.classify_gray_hist()
#     # print(degree1)
#     # degree2 = ic.calculate()
#     # print(degree2)
#     # degree3 = ic.classify_hist_with_split()
#     # print(degree3)
#     degree4 = ic.classify_aHash()
#     print(degree4)
